//
//  HomeViewController.m
//  VNBooks
//
//  Created by hainguyen on 12/15/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "HomeViewController.h"
#import "VNBooksAppDelegate.h"
#import "Chapter.h"
#import "ChapterExtension.h"
#import "AppValues.h"

@implementation HomeViewController
@synthesize homeTable;
@synthesize chapterCell;
@synthesize titleView;
@synthesize titleImageView;
@synthesize chapterList;

#pragma mark -
#pragma UITableView Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [chapterList count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 61;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *homeCellId = @"homeCellID";
    chapterCell = (ChapterCell*)[tableView dequeueReusableCellWithIdentifier:homeCellId];
    if (chapterCell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"ChapterCell" owner:self options:nil];
    }
    Chapter *chapter = [chapterList objectAtIndex:indexPath.row];
    if ([indexPath row] == 0) {
        chapterCell.backgroundImage.image = [UIImage imageNamed:@"chapter_unselected_top_cell.png"];
        chapterCell.cellIndex = first;
    }
    else if ([indexPath row] == ([chapterList count]-1))
    {
        chapterCell.backgroundImage.image = [UIImage imageNamed:@"chapter_unselected_bottom_cell.png"];
        chapterCell.cellIndex = last;
    }
    else{
        chapterCell.backgroundImage.image = [UIImage imageNamed:@"chapter_unselected_middle_cell.png"];
        chapterCell.cellIndex = middle;
    }
    chapterCell.chapterNameLabel.text = chapter.name;
    chapterCell.excerptLabel.text = chapter.excerpt;
    return [chapterCell autorelease];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%i",[indexPath row]);
    ChapterDetailViewController    *chapterDetailController = [[ChapterDetailViewController alloc] init];
    Chapter *chapter = [chapterList objectAtIndex:indexPath.row];
    [mainAppDelegate.mainNavController pushViewController:chapterDetailController animated:YES];
    [chapterDetailController loadContentWithChapterId:chapter.id];
    [chapterDetailController release];
    NSLog(@"Push View Controller");
}

#pragma mark -

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    chapterList = [[NSMutableArray alloc] init];
    [chapterList setArray:[Chapter getChapters]];
    [homeTable reloadData];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc{
    [homeTable release];
    [chapterCell release];
    [titleView release];
    [titleImageView release];
    [chapterList release];
    [super dealloc];
}

@end
